//
//  PersonalPublishAlertView.swift
//  XGamesLive
//
//  Created by ios on 2024/5/21.
//

import UIKit

class PersonalPublishAlertView: UIView {
    
    private var contentView: UIView!

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.createXglView()
        self.backgroundColor = UIColor.black.withAlphaComponent(0.3)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        self.layoutIfNeeded()
        self.contentView.addCorner(conrners: [.topLeft,.topRight], radius: 10.0)
    }
    
    func showPickerView(){
 
        self.layoutIfNeeded()
        UIView.animate(withDuration: 0.25) {
            self.contentView.transform = CGAffineTransform(translationX: 0, y: -self.contentView.bounds.height)
        }
    }
    
    func dismiss(){
        self.removeFromSuperview()
    }
}

extension PersonalPublishAlertView{
    @objc private func selectLiveItemAction(){
        guard let window = UIApplication.shared.currentWindow,let controller = window.visibleViewController() else { return  }
        let vc = XGL_PersonalPublishXGL_LiveViewController()
        controller.navigationController?.pushViewController(vc)
        self.dismiss()
    }
    @objc private func selectImageItemAction(){
        guard let window = UIApplication.shared.currentWindow,let controller = window.visibleViewController() else { return  }
        let vc = XGL_PersonalPublishPhotoViewController()
        controller.navigationController?.pushViewController(vc)
        self.dismiss()
    }
    @objc private func selectVideoItemAction(){
        guard let window = UIApplication.shared.currentWindow,let controller = window.visibleViewController() else { return  }
        let vc = XGL_PersonalPublishVideoViewController()
        controller.navigationController?.pushViewController(vc)
        self.dismiss()
    }
    @objc private func dismissAction(){
        self.dismiss()
    }
}

extension PersonalPublishAlertView{
    private func createXglView(){
        self.contentView = UIView().then({ make in
            make.backgroundColor = UIColor.hexColor("#001310")
        })
        let liveButton = UIButton().then { make in
            make.addTarget(self, action: #selector(self.selectLiveItemAction), for: .touchUpInside)
        }
        let liveItem = XGLPublishUIManager.createButtonItem(title: "Room", icon: "icon_xgl_publish_live_type", button: liveButton)
        let imageButton = UIButton().then { make in
            make.addTarget(self, action: #selector(self.selectImageItemAction), for: .touchUpInside)
        }
        let imageItem = XGLPublishUIManager.createButtonItem(title: "Photo", icon: "icon_xgl_publish_image_type", button: imageButton)
        let videoButton = UIButton().then { make in
            make.addTarget(self, action: #selector(self.selectVideoItemAction), for: .touchUpInside)
        }
        let videoItem = XGLPublishUIManager.createButtonItem(title: "Video", icon: "icon_xgl_publish_video_type", button: videoButton)
       
        let closeItem = UIButton().then { make in
            make.setImage(imageWithName("icon_xgl_alert_close"), for: .normal)
            make.addTarget(self, action: #selector(self.dismissAction), for: .touchUpInside)
        }
        self.contentView.addSubviews([liveItem,imageItem,videoItem,closeItem])
        self.addSubview(self.contentView)
        liveItem.snp.makeConstraints { make in
            make.centerY.width.equalTo(imageItem)
            make.right.equalTo(imageItem.snp.left).offset(-48.0)
        }
        imageItem.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(conversionWithScrennWidth(value: 62.0))
            make.width.equalTo(conversionWithScrennWidth(value: 61.0))
        }
        videoItem.snp.makeConstraints { make in
            make.centerY.width.equalTo(imageItem)
            make.left.equalTo(imageItem.snp.right).offset(48.0)
        }
        closeItem.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(imageItem.snp.bottom).offset(conversionWithScrennWidth(value: 56.0))
            make.width.height.equalTo(conversionWithScrennWidth(value: 61.0))
        }
        self.contentView.snp.makeConstraints { make in
            make.width.centerX.equalToSuperview()
            make.bottom.equalTo(closeItem.snp.bottom).offset(BottomHeight + 40.0)
            make.top.equalTo(self.snp.bottom)
        }
    }
}
